<?php
/**
 * Feedback_model.php
 *
 * $Id:$
 */
 
class Feedback_model extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * 
	 * get feedback with school catalog by project id
	 *
	 * @param int $project_id
	 * @return array
	 */
	function get_school_merge_list($project_id_list)
	{
		
		// get feedback list
		$feedback_list = $this->db
			->from('project_feedback')
			->where_in('p_id', $project_id_list)
			->get()
			->result_array();
		
		// merge result
		return $this->_merge_school($feedback_list);
		
	}
	
	function _merge_school($feedback_list)
	{
		if($feedback_list == NULL)
		{
			return NULL;
		}
		
		// merged feedback list
		$merged_list = array();
		foreach($feedback_list as $feedback)
		{
			
			// project + school first came out
			if(!array_key_exists($feedback['p_id'], $merged_list)
			|| !array_key_exists($feedback['s_id'], $merged_list[$feedback['p_id']]))
			{
				$merged_list[$feedback['p_id']][$feedback['s_id']] = array(
					'enter_status' => $feedback['enter_status'],
					'enter_total' => 0
				);
			}
			
			$current =& $merged_list[$feedback['p_id']][$feedback['s_id']];
			
			// enter status
			if($current['enter_status'] != OA_CONST_FEEDBACK::ENTER_SUCCESS
			&& $current['enter_status'] != $feedback['enter_status']
			&& $feedback['enter_status'] != OA_CONST_FEEDBACK::ENTER_NO_TRY)
			{
				$current['enter_status'] = $feedback['enter_status'];
			}
			
			// enter_total
			$current['enter_total'] += (int)$feedback['enter_grade_1'] 
				+ (int)$feedback['enter_grade_2'] 
				+ (int)$feedback['enter_grade_3'];
			
		}
		
		return $merged_list;
	}
	
	/**
	 * 
	 * get feedback with member order by project id
	 *
	 * @param int $project_id_list
	 * @return array
	 */
	function get_member_merge_list($project_id_list)
	{
		// get feedback list
		$feedback_list = $this->db
			->from('project_feedback')
			->where_in('p_id', $project_id_list)
			->get()
			->result_array();
		
		// merge result
		return $this->_merge_member($feedback_list);
	}
	
	function _merge_member($feedback_list)
	{
		if($feedback_list == NULL)
		{
			return NULL;
		}
		
		// merged feedback list
		$merged_list = array();
		foreach($feedback_list as $feedback)
		{
			$merged_list[$feedback['p_id']][$feedback['m_id']][$feedback['s_id']] 
				= $feedback + array(
					'enter_total' => 
					$feedback['enter_grade_1'] +
					$feedback['enter_grade_2'] + $feedback['enter_grade_3']);
		}
		
		return $merged_list;
	}
	
}

/* End file of Feedback_model.php */
/* Location: Feedback_model.php */